package org.dromara.authcode.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.dromara.authcode.service.IAuthorizedService;
import org.dromara.common.core.enums.FormatsType;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.encrypt.utils.EncryptUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class AuthorizedServiceImpl implements IAuthorizedService {
    private static final String AUTHORIZED_CODE_SEPARATOR = "|"; // 授权码分隔符
    private static final int DIFF_TIME = 30; // 时间差
    @Value("${app.aesKey}")
    private String authorized_aseKey;
    @Value("${app.aesEnable}")
    private boolean aesEnable;

    /**
     * 转换授权码，获取Mac地址
     * 格式 UUID|dateTime|authCode
     *
     * @param code 授权码
     * @return 授权码
     */
    @Override
    public String convertAuthorizedCode(String code) {
        List<String> list =getAuthorizedCodeList(code);
        if (!list.isEmpty()) {
            return list.getFirst();
        }
        return null;
    }

    /**
     * 获取授权码列
     * 格式 UUID|dateTime|authCode
     *
     * @param code 授权码
     * @return 授权码
     */
    @Override
    public String getAuthCode(String code) {
        List<String> list =getAuthorizedCodeList(code);
        if (!list.isEmpty() && list.size() > 2) {
            return list.get(2);
        }
        return null;
    }

    /**
     * 获取Mac地址
     * 格式 UUID|dateTime|authCode
     *
     * @param code 授权码
     * @return 授权码
     */
    @Override
    public String getMacAddress(String code) {
        List<String> list =getAuthorizedCodeList(code);
        if (!list.isEmpty()) {
            return list.getFirst();
        }
        return null;
    }
    /**
     * 获取授权码List
     *
     * @param code 授权码
     * @return 授权码List
     */
    @Override
    public List<String> getAuthorizedCodeList(String code) {
        return StringUtils.splitList(code, AUTHORIZED_CODE_SEPARATOR);
    }
    /**
     * 解密授权码
     *
     * @param code 加密的授权码
     * @return 授权码
     */
    @Override
    public String decryptAuthorizedCode(String code) {
        if (aesEnable) {
            return EncryptUtils.decryptByAes(code, authorized_aseKey);
        }
        return code;
    }

    /**
     * 加密授权码
     *
     * @param code 授权码
     * @return 加密的授权码
     */
    @Override
    public String encryptAuthorizedCode(String code) {
        if (aesEnable) {
            return EncryptUtils.encryptByAes(code, authorized_aseKey);
        }
        return code;
    }

    /**
     * 检查授权码时间校验
     * 与现在时间间隔小于30秒
     * @param code 授权码
     * @return 是否有效
     */
    @Override
    public boolean checkDateTime(String code) {
        List<String> list = StringUtils.splitList(code, AUTHORIZED_CODE_SEPARATOR);
        if (list.size() > 1) {
            String dateTime = list.get(1);
            if (StringUtils.isNotBlank(dateTime)) {
                Date checkDateTime = DateUtils.parseDateTime(FormatsType.YYYYMMDDHHMMSS, dateTime);
                return DateUtils.difference(checkDateTime, new Date(), TimeUnit.SECONDS) > DIFF_TIME;
            } else {
                log.error("授权码时间格式错误");
                return false;
            }
        }
        return false;
    }

}
